package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.RecordEngin;
import com.ruoyi.system.domain.vo.BarChartVo;
import com.ruoyi.system.service.IRecordEnginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 项目工程管理Controller
 *
 * @author admin
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/system/engin")
public class RecordEnginController extends BaseController {
    @Autowired
    private IRecordEnginService recordEnginService;

    /**
     * 查询项目工程管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:engin:list')")
    @GetMapping("/list")
    public TableDataInfo list(RecordEngin recordEngin) {
        startPage();
        List<RecordEngin> list = recordEnginService.selectRecordEnginList(recordEngin);
        return getDataTable(list);
    }

    /**
     * 查询项目工程管理列表
     */
    @GetMapping("/listByName")
    public TableDataInfo listEnginByName(RecordEngin recordEngin) {
        List<RecordEngin> list = recordEnginService.selectRecordEnginListByName(recordEngin);
        return getDataTable(list);
    }

    /**
     * 导出项目工程管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:engin:export')")
    @Log(title = "项目工程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RecordEngin recordEngin) {
        List<RecordEngin> list = recordEnginService.selectRecordEnginList(recordEngin);
        ExcelUtil<RecordEngin> util = new ExcelUtil<RecordEngin>(RecordEngin.class);
        util.exportExcel(response, list, "项目工程管理数据");
    }

    /**
     * 获取项目工程管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:engin:query')")
    @GetMapping(value = "/{enginId}")
    public AjaxResult getInfo(@PathVariable("enginId") Long enginId) {
        return AjaxResult.success(recordEnginService.selectRecordEnginByEnginId(enginId));
    }

    /**
     * 新增项目工程管理
     */
    @PreAuthorize("@ss.hasPermi('system:engin:add')")
    @Log(title = "项目工程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RecordEngin recordEngin) {
        return toAjax(recordEnginService.insertRecordEngin(recordEngin));
    }

    /**
     * 修改项目工程管理
     */
    @PreAuthorize("@ss.hasPermi('system:engin:edit')")
    @Log(title = "项目工程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RecordEngin recordEngin) {
        return toAjax(recordEnginService.updateRecordEngin(recordEngin));
    }

    /**
     * 删除项目工程管理
     */
    @PreAuthorize("@ss.hasPermi('system:engin:remove')")
    @Log(title = "项目工程管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{enginIds}")
    public AjaxResult remove(@PathVariable Long[] enginIds) {
        return toAjax(recordEnginService.deleteRecordEnginByEnginIds(enginIds));
    }

    /**
     * 项目工程导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<RecordEngin> util = new ExcelUtil<RecordEngin>(RecordEngin.class);
        util.importTemplateExcel(response, "项目工程数据");
    }

    /**
     * 项目工程导入
     */
    @Log(title = "项目工程导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:engin:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<RecordEngin> util = new ExcelUtil<RecordEngin>(RecordEngin.class);
        List<RecordEngin> enginList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = recordEnginService.importEngin(enginList, operName);
        return AjaxResult.success(message);
    }

    // 柱状图，饼状图数据
    @GetMapping("/bar")
    public TableDataInfo bar(BarChartVo barChartVo) {
        List<BarChartVo> list = recordEnginService.selectBarChartVoList(barChartVo);
        return getDataTable(list);
    }
}
